package com.hejian.android.a72ggametools.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.hejian.android.a72ggametools.R;
import com.hejian.android.a72ggametools.bean.GameDetails;
import com.hejian.android.a72ggametools.bean.GiftDetail;
import com.hejian.android.a72ggametools.common.Constant;
import com.hejian.android.a72ggametools.imageloader.DoubleCache;
import com.hejian.android.a72ggametools.imageloader.ImageCache;
import com.hejian.android.a72ggametools.imageloader.ImageLoader;
import com.hejian.android.a72ggametools.net.HttpUtils;
import com.hejian.android.a72ggametools.utils.JsonParse;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/1/22.
 */
public class GiftDetailActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "hejiandebug";
    private String id;
    private ImageCache imageCache = new DoubleCache();
    private ImageLoader imageLoader;
    private ImageView backBtn;
    private ImageView shareBtn;
    private ImageView imgIcon;
    private TextView tvPrice;
    private TextView tvName;
    private TextView tvRemain;
    private ProgressBar progressRemain;
    private TextView tvTime;
    private TextView giftContent;
    private TextView giftGetMethod;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gift_detail);
        imageLoader = ImageLoader.getInstance(getApplicationContext(),imageCache);
        initView();
        initData();
        setListener();
    }

    private void setListener() {
        backBtn.setOnClickListener(this);
        shareBtn.setOnClickListener(this);
    }

    private void initView() {
        Log.d(TAG, "initView: ..........");
        backBtn = (ImageView)findViewById(R.id.gift_detail_back);
        shareBtn = (ImageView)findViewById(R.id.gift_detail_share);
        imgIcon = (ImageView)findViewById(R.id.iv_icon);
        tvPrice = (TextView)findViewById(R.id.tv_price);
        tvName = (TextView)findViewById(R.id.tv_name);
        tvRemain = (TextView)findViewById(R.id.tv_remain);
        progressRemain = (ProgressBar)findViewById(R.id.pb_remain);
        tvTime = (TextView)findViewById(R.id.tv_time);
        giftContent = (TextView)findViewById(R.id.tv_lbnr);
        giftGetMethod = (TextView)findViewById(R.id.tv_lqfs);

    }

    private void initData() {
        Log.d(TAG, "initData: .............");
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        Log.d(TAG, "initData: ........."+id);
        loadGiftDetailPage();

    }

    private void loadGiftDetailPage() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                loadGiftDetail();
            }
        }).start();
    }

    private void loadGiftDetail() {
        Log.d(TAG, "loadGiftDetail: ......"+Thread.currentThread().getId());
        Map<String,String> map = new HashMap<String, String>();
        map.put("id",id);
        String buffer = HttpUtils.doPostRequest(Constant.GAME_GIFT_URL,map);
        GiftDetail detail = JsonParse.parseGiftDetailJson(buffer);
        Log.d(TAG, "loadGiftDetail: ......"+ detail.toString());
        if (detail != null){
            Log.d(TAG, "loadGiftDetail: .....12345");
            Message msg = Message.obtain();
            msg.what = 1;
            msg.obj = detail;
            handler.sendMessage(msg);
        }
    }

    private GiftDetail giftDetail;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1){
                Log.d(TAG, "handleMessage: ......");
                giftDetail = (GiftDetail) msg.obj;
                imageLoader.displayImage(giftDetail.getIcon(),imgIcon);
                tvPrice.setText(giftDetail.getConsume()+"U币");
                tvName.setText(giftDetail.getName());
                tvRemain.setText(giftDetail.getRemain()+"/"+giftDetail.getTotal());
                progressRemain.setMax(Integer.valueOf(giftDetail.getTotal()));
                progressRemain.setProgress(Integer.valueOf(giftDetail.getRemain()));
                tvTime.setText(giftDetail.getsTime()+" - "+giftDetail.geteTime());
                giftContent.setText(giftDetail.getContent());
                giftGetMethod.setText(giftDetail.getHowget());
            }
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.gift_detail_back:
                finish();
                break;
            case R.id.gift_detail_share:
                String share = giftDetail.getAndroidDl();
                Intent intent = new Intent("android.intent.action.SEND");
                intent.putExtra(Constant.SHARE_SINA_WEIBO_ACTION, share);
                intent.putExtra(Constant.SHARE_TENCENT_MSC_ACTION, share);
                intent.putExtra(Constant.SHARE_TENCENT_MSC_TIME_LINE_ACTION, share);
                intent.putExtra(Constant.SHARE_TENCENT_QQ_ACTION, share);
                startActivity(Intent.createChooser(intent, "分享到"));
                break;
        }
    }
}
